/*
 * netfun.cpp
 *
 *  Created on: 2021年1月18日
 *      Author: Admin
 */


#include <fstream>
#include <sstream>
#include "restclient-cpp/restclient.h"
#include "json/json.h"
//#include"utils/log.h"
#include "security/SecurityManager.h"
#include "net/NetManager.h"
#include "manager/ConfigManager.h"
#include <system/Mutex.h>
#include "os/UpgradeMonitor.h"
#include <unistd.h>
#include "utils/timeHelper.h"

#include "utils/GpioHelper.h"

#include "utils/Log.h"
bool syncWeather();
bool syncNetTime(){
	RestClient::Response r = RestClient::get("http://quan.suning.com/getSysTime.do");
	if(200 == r.code){
		Json::Reader reader;
		Json::Value root;
		if(!reader.parse(r.body, root)){
			LOGE("sync() Parse failed!!");
			return false;
		}

		if (root.isMember("sysTime2")) {
			LOGD("sysTime2 = %s", root["sysTime2"].asString().c_str());
			char l_temp[128] = {0};
			memcpy( l_temp, root["sysTime2"].asString().c_str(), strlen(root["sysTime2"].asString().c_str()) );
//			char *split = strchr(l_temp, ' ');
//			char l_data[32] = {0};
//			char l_time[32] = {0};
//			memcpy(l_data, l_temp, (int)(split - l_temp) );
//			memcpy(l_time, split+1, (int)(l_temp - split) );

			struct tm time;
			char *p = strtok(l_temp, "-");
			if(!p)
				return true;
			time.tm_year = atoi(p) - 1900;

			p = strtok(NULL, "-");
			if(!p)
				return true;
			time.tm_mon = atoi(p) - 1;

			p = strtok(NULL, " ");
			if(!p)
				return true;
			time.tm_mday = atoi(p);

			p = strtok(NULL, ":");
			if(!p)
				return true;
			time.tm_hour = atoi(p);

			p = strtok(NULL, ":");
			if(!p)
				return true;
			time.tm_min = atoi(p);

			p = strtok(NULL, ":");
			if(!p)
				return true;
			time.tm_sec = atoi(p);

			LOGD("Time = %04d-%02d-%02d %02d:%02d:%02d",
					time.tm_year+1900, time.tm_mon+1, time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec);

			TimeHelper::setDateTime(&time);
		}

	}else{
		LOGD("UpdateTime r.code = %d !!!\n", r.code);
		return false;
	}
	syncWeather();
	return true;
}
#include "uart/ProtocolData.h"

bool syncWeather(){
	RestClient::Response r = RestClient::get("http://api.seniverse.com/v3/weather/now.json?key=SYvb5AJcitrnXCdQd&location=shenzhen&language=zh-Hans&unit=c");
	if(200 == r.code){
		Json::Reader reader;
		Json::Value root;
		if(!reader.parse(r.body, root)){
			LOGE("sync() Parse failed!!");
			return false;
		}
		LOGD("UpdateTime r.body = %s !!!\n", r.body.c_str());
		if (root.isMember("results")) {
			LOGD("results = %s \n", root["results"].toStyledString().c_str());
			if(root["results"][0].isMember("now")){
				LOGD("now = %s \n", root["results"][0]["now"].toStyledString().c_str());

				SProtocolData* mp = &getProtocolData();
				mp->weather.code = atoi(root["results"][0]["now"]["code"].asCString());
				memset(mp->weather.desp,0,sizeof(mp->weather.desp));
				strcpy(mp->weather.desp, root["results"][0]["now"]["text"].asCString());
				mp->weather.temputre =  atoi(root["results"][0]["now"]["temperature"].asCString());
				LOGD("code:[%d] text[%s] temp[%d]\n",mp->weather.code,mp->weather.desp,mp->weather.temputre);
			}
		}

	}else{
		LOGD("Update weathers\ r.code = %d !!!\n", r.code);
	}
	return true;
}
